import ajax from '../../../js/ajax.js';

let bj=document.getElementById('bj');
let xs=document.getElementById('xs');

let bjsz=[];
let xssz=[];

function bjHQ(){
  ajax.send('/linkinfo/queryAllClass',{},(data)=>{
    if (data.success) {
      bjsz = data.list;
      bjZS();
      return;
    }
    alert(data.message);
  });
  
}

function bjZS(){
  console.log('班级列表', bjsz);
  for (let i = 0; i < bjsz.length; i++) {
    let p = bjsz[i];
    let op = document.createElement('option');
    op.setAttribute('value', p.cid);
    op.append(p.cname);
    bj.append(op);
  }
  if (bjsz.length > 0) {
    bj.selectedIndex = parseInt(bjsz.length / 2);
    //数据变化触发联动
    xsHQ();
  }
}

function xsHQ(){
  let cid=bj.value;
  ajax.send('/linkinfo/queryStudentByClass',{cid:cid},(data)=>{
    if (data.success) {
      xssz = data.list;
      xsZS();
      return;
    }
    alert(data.message);
  })
}

function xsZS(){
  xs.innerHTML='';
  for (let i = 0; i < xssz.length; i++) {
    let x = xssz[i];
    let op = document.createElement('option');
    op.setAttribute('value', x.sid);
    op.append(x.sname);
    xs.append(op);
  }
  if (xssz.length > 0) {
    xs.selectedIndex = parseInt(xssz.length / 2);
  }
}

bj.addEventListener('change', xsHQ);
bjHQ();

let Ok=document.getElementById('Ok');
let Info=document.getElementById('Info');

Ok.addEventListener('click',()=>{
  Info.innerHTML='';
  let pindex=bj.selectedIndex;
  if(pindex>-1){
    let b=bjsz[pindex];
    Info.append(b.cname);
  }

  let cindex=xs.selectedIndex;
  if(cindex>-1){
    let x=xssz[cindex];
    Info.append(x.sname);
  }
});


